package com.itheima.ssm.controller;

import com.itheima.ssm.domain.Permission;
import com.itheima.ssm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//资源权限
@Controller
@RequestMapping(path = "/permission")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;

    @RequestMapping(path = "/findAll")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Permission> permissionList = iPermissionService.findAll();
        mv.addObject("permissionList", permissionList);
        mv.setViewName("permission-list");
        return mv;
    }

    //    权限添加
    @RequestMapping(path = "/save")
    public String save(Permission permission) throws Exception {
        iPermissionService.save(permission);

        return "redirect:findAll";
    }

    //    资源权限详情
    @RequestMapping(path = "/findById")
    public ModelAndView findById(String id) throws Exception {
        ModelAndView mv = new ModelAndView();
        Permission permission = iPermissionService.findById(id);
        mv.addObject("permission", permission);
        mv.setViewName("permission-show");
        return mv;
    }

    //    删除资源权限
    @RequestMapping(path = "/deletePermission")
    public String deletePermission(String id) throws Exception {
        iPermissionService.deletePermission(id);
        return "redirect:findAll";
    }

}
